package 算法.leetcode.bytedance.字符串;

import java.util.Stack;

/**
 * @author lchenglong
 * @date 2023/2/14
 */
public class 简化路径 {
    public static class Solution {

        public static void main(String[] args) {
            Solution solution = new Solution();
            String s = solution.simplifyPath("///home/chenglong/../../develop/code///./redis/");
            System.out.println(s);
        }
        public String simplifyPath(String path) {
            String[] array = path.split("\\/");
            Stack<String> stack = new Stack<>();
            for (String s:array){
                if (s.equals(".")||s.equals(""))continue;
                if (s.equals("..")){
                    if (!stack.empty())stack.pop();
                    continue;
                }
                stack.push(s);
            }
            if (stack.empty())return "/";
            StringBuilder ans = new StringBuilder();
            while (!stack.empty()){
                ans.insert(0,"/" + stack.pop());
            }
            return ans.toString();
        }
    }
}
